package com.example.fbi_text.views;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.fbi_text.Acitivity.CartActivity;
import com.example.fbi_text.Acitivity.MainNewsActivity;
import com.example.fbi_text.Acitivity.SettingsActivity;
import com.example.fbi_text.R;
import com.example.fbi_text.base.BaseFragment;
import com.example.fbi_text.bean.User;
import com.example.fbi_text.database.DBHelper;
import com.example.fbi_text.utils.SpUtils;

public class Myselffragment extends BaseFragment implements View.OnClickListener {
    private RelativeLayout settingsLayout;
    private ImageView order_arrow_icon;
    private RelativeLayout shoppingcartLayout;
    private RelativeLayout my_text;
    private TextView my_name;
    private TextView my_id;
    private TextView my_email;
    private TextView my_code;
    private SpUtils mSpUtils;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_myself;
    }

    @Override
    protected void initView() {
        settingsLayout = mView.findViewById(R.id.settingView);
        shoppingcartLayout = mView.findViewById(R.id.Shoppingcart_View);
        order_arrow_icon=mView.findViewById(R.id.order_arrow_icon);
        my_text = mView.findViewById(R.id.my_write);
        my_name = mView.findViewById(R.id.my_name);
        my_id = mView.findViewById(R.id.my_id);
        my_email = mView.findViewById(R.id.my_email);
        my_code = mView.findViewById(R.id.my_code);
        mSpUtils = new SpUtils(mContext);

        // 加载用户信息
        loadUserInfo();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 每次Fragment显示时刷新用户信息
        loadUserInfo();
    }

    private void loadUserInfo() {
        // 查询当前登录帐号的id
        int loginId = mSpUtils.getInt(SpUtils.LOGIN_KEY, -1);
        if (loginId >= 0) {
            // 根据id查询当前登录用户的信息
            User user = DBHelper.getInstance(mContext).getUserById(loginId);
            if (user != null) {
                // 将当前登录的用户信息显示在界面上
                my_name.setText(user.getUser_name());
                my_id.setText(String.valueOf(user.getUser_id()));
                my_email.setText(user.getUser_email());
                my_code.setText(user.getUser_password());
            }
        }
    }

    @Override
    protected void initListener() {
        settingsLayout.setOnClickListener(this);
        order_arrow_icon.setOnClickListener(this);
        shoppingcartLayout.setOnClickListener(this);
        my_text.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.settingView) {
            Intent intent = new Intent(mContext, SettingsActivity.class);
            startActivity(intent);
        }else if(id==R.id.order_arrow_icon){
            Intent intent = new Intent(mContext, CartActivity.class);
            startActivity(intent);
        }else if (id == R.id.Shoppingcart_View) {
            Intent intent = new Intent(mContext, CartActivity.class);
            startActivity(intent);
        } else if (id == R.id.my_write) {
            Intent intent = new Intent(mContext, MainNewsActivity.class);
            startActivity(intent);
        }
    }
}
